package com.gexiaobao.pigeon.app

import android.content.Context
import android.content.Intent
import android.util.Log
import com.alibaba.sdk.android.push.MessageReceiver
import com.alibaba.sdk.android.push.notification.CPushMessage
import com.gexiaobao.pigeon.app.ext.Constant
import com.gexiaobao.pigeon.app.model.bean.User
import com.gexiaobao.pigeon.app.model.bean.UserEvent
import com.gexiaobao.pigeon.app.util.CacheUtil
import com.gexiaobao.pigeon.app.util.KvUtils
import com.gexiaobao.pigeon.app.util.RxBus
import com.gexiaobao.pigeon.ui.activity.ActivityGameResult
import com.gexiaobao.pigeon.ui.activity.TestInputActivity
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.util.startActivity
import org.json.JSONObject

/**
 * @author: 正纬
 * @since: 15/4/9
 * @version: 1.1
 * @feature: 用于接收推送的通知和消息
 */
class MyMessageReceiver : MessageReceiver() {
    /**
     * 推送通知的回调方法
     *
     * @param context
     * @param title
     * @param summary
     * @param extraMap
     */
    public override fun onNotification(context: Context, title: String, summary: String, extraMap: Map<String, String>) {
        // 处理推送通知
        if (null != extraMap) {
            Log.i(REC_TAG, "@收到通知${extraMap.size}")
//            val user = User("小明", 15)
//            RxBus.send(UserEvent(user))
//            for ((key, value) in extraMap) {
//                Log.i(REC_TAG, "@Get diy param : Key=$key , Value=$value")
//            }
        } else {
            Log.i(REC_TAG, "@收到通知 && 自定义消息为空")
        }
        Log.i(REC_TAG, "收到一条推送通知 ： $title, summary:$summary")
    }

    /**
     * 应用处于前台时通知到达回调。注意:该方法仅对自定义样式通知有效,相关详情请参考https://help.aliyun.com/document_detail/30066.html?spm=5176.product30047.6.620.wjcC87#h3-3-4-basiccustompushnotification-api
     *
     * @param context
     * @param title
     * @param summary
     * @param extraMap
     * @param openType
     * @param openActivity
     * @param openUrl
     */
    override fun onNotificationReceivedInApp(
        context: Context,
        title: String,
        summary: String,
        extraMap: Map<String, String>,
        openType: Int,
        openActivity: String,
        openUrl: String
    ) {
        Log.i(REC_TAG, "onNotificationReceivedInApp ：  : $title : $summary  $extraMap : $openType : $openActivity : $openUrl")
    }

    /**
     * 推送消息的回调方法
     *
     * @param context
     * @param cPushMessage
     */
    public override fun onMessage(context: Context, cPushMessage: CPushMessage) {
        Log.i(REC_TAG, "收到一条推送消息 ： " + cPushMessage.title + ", content:" + cPushMessage.content)
    }

    /**
     * 从通知栏打开通知的扩展处理
     *
     * @param context
     * @param title
     * @param summary
     * @param extraMap
     */
    public override fun onNotificationOpened(context: Context, title: String, summary: String, extraMap: String) {
        val token = CacheUtil.getToken()
        val uid = KvUtils.decodeString(Constant.USERID)
        val jsonObject = JSONObject(extraMap)
        val pushType = jsonObject.optString("pushType")
        if (pushType == "4") {//奖金明细查看

        }
        if (pushType == "3") {//聊天
            val channelId = jsonObject.optString("channelId")
            val intent = Intent(context, TestInputActivity::class.java)
            intent.putExtra("channelId", channelId)
            intent.putExtra("token", token)
            intent.putExtra("uid", uid)
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
        if (pushType == "2") {//订单

        }
        if (pushType == "1") {//比赛
//            val raceId = jsonObject.optString("raceId")
//            val pigeonId = jsonObject.optString("pigeonId")
//            val ringId = jsonObject.optString("ringId")
            val passId = jsonObject.optString("passId")
            val memberNum = jsonObject.optString("memberNum")
            val intent = Intent(context, ActivityGameResult::class.java)
            intent.putExtra("title", "")
            intent.putExtra("passId", passId.toInt())
            intent.putExtra("type", "race")
            intent.putExtra("memberNum", memberNum)
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
    }

    /**
     * 通知删除回调
     *
     * @param context
     * @param messageId
     */
    public override fun onNotificationRemoved(context: Context, messageId: String) {
        Log.i(REC_TAG, "onNotificationRemoved ： $messageId")
    }

    /**
     * 无动作通知点击回调。当在后台或阿里云控制台指定的通知动作为无逻辑跳转时,通知点击回调为onNotificationClickedWithNoAction而不是onNotificationOpened
     *
     * @param context
     * @param title
     * @param summary
     * @param extraMap
     */
    override fun onNotificationClickedWithNoAction(context: Context, title: String, summary: String, extraMap: String) {
        Log.i(REC_TAG, "onNotificationClickedWithNoAction ：  : $title : $summary : $extraMap")
    }

    companion object {
        // 消息接收部分的LOG_TAG
        const val REC_TAG = "receiver"
    }
}